package com.chensheng.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service
public class RedisService {

    @Autowired
    JedisPool jedisPool;

    public void set(String key, String value){
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            jedis.set(key, value);
        }finally{
            if(jedis != null){
                jedis.close();
            }
        }
    }

    public String get(String key){
        Jedis jedis = null;
        String value = null;
        try{
            jedis = jedisPool.getResource();
            value = jedis.get(key);
        }finally {
            if(jedis != null){
                jedis.close();
            }
            return value;
        }
    }

    public void delete(String key) {
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            jedis.del(key);
        }finally{
            if(jedis != null){
                jedis.close();
            }
        }
    }
}
